package gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.util.Arrays;
import java.util.TreeSet;

import data.RatingTuple;
import logic.Facade;
import logic.ToGuiFacade;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class pnlRecommendationList extends javax.swing.JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel picLabel;
	private JButton next10Button;
	private JButton backButton;
	private JButton previousButton;
	private JButton searchButton;
	private JButton allButton;
	private JTextArea headlineTextArea;
	private JTextArea searchTextArea;
	private JLabel[] _movieLabels;
	private JLabel[] _scoresLabels;
	private JLabel[] _numLabels;
	private JButton[] _rateButtons;
	private String _user;
	private String[] _allMovies;
	private String[] _searchMovies;
	private JTextField searchTextField;

	public pnlRecommendationList(String user) {
		super();
		_user = user;
		initGUI();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(699, 489));
			this.setBackground(new java.awt.Color(255,128,0));
			this.setLayout(null);
			this.setFont(new java.awt.Font("Arial",0,12));
			{
				picLabel = new JLabel();
				this.add(picLabel);
				picLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("documentation/miniSecret4PlusDog.JPG")));
				picLabel.setBounds(555, 29, 132, 237);
			}
			{
				headlineTextArea = new JTextArea();
				this.add(headlineTextArea);
				headlineTextArea.setText("Rate a movie");
				headlineTextArea.setBounds(209, 29, 131, 27);
				headlineTextArea.setOpaque(false);
				headlineTextArea.setFont(new java.awt.Font("Arial",1,20));
				headlineTextArea.setEditable(false);
			}
			_numLabels = new JLabel[10];
			for(int i=0 ; i<10 ; i++) {
				_numLabels[i] = new JLabel();
				this.add(_numLabels[i]);
				_numLabels[i].setBounds(75, 86+30*i, 30, 16);
				_numLabels[i].setFont(new java.awt.Font("Tahoma",1,14));
			}
			{
				_scoresLabels = new JLabel[10];
				for (int i=0 ; i<10 ; i++){
					_scoresLabels[i] = new JLabel();
					this.add(_scoresLabels[i]);
					_scoresLabels[i].setFont(new java.awt.Font("Tahoma",1,14));

					_scoresLabels[i].setBounds(420, 86+30*i, 50, 14);
				}
			}
			{
				_movieLabels = new JLabel[10];
				for (int i=0 ; i<10 ; i++){
					_movieLabels[i] = new JLabel();
					this.add(_movieLabels[i]);
					_movieLabels[i].setFont(new java.awt.Font("Tahoma",1,14));

					_movieLabels[i].setBounds(109, 86+30*i, 407, 18);

					final JLabel tmp = _movieLabels[i];
					_movieLabels[i].addMouseListener(new MouseAdapter() {
						public void mouseEntered(MouseEvent evt) {
							tmp.setForeground(Color.CYAN);

						}
						public void mouseExited(MouseEvent evt) {
							tmp.setForeground(Color.DARK_GRAY);
						}
						public void mouseReleased(MouseEvent evt){
							ToGuiFacade.getInstance().changeScreen(new pnlAddRating(_user, tmp.getText()));
						}
					});
				}
			}
			{
				_rateButtons = new JButton[10];
				for (int i=0; i<10; i++){
					_rateButtons[i]=new JButton("View");
					this.add(_rateButtons[i]);
					_rateButtons[i].setBounds(10, 86+30*i, 60, 14);
					_rateButtons[i].setFont(new java.awt.Font("Tahoma",1,11));
					final int tmpnum=i;
					_rateButtons[i].addMouseListener(new MouseAdapter() {
						public void mouseReleased(MouseEvent evt) {
							rateButtonActionPerformed(evt,tmpnum);
						}
					});
				}
			}
			{
				next10Button = new JButton();
				this.add(next10Button);
				next10Button.setText("Next 10 movies");
				next10Button.setBounds(243, 395, 191, 21);
				next10Button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						next10ButtonActionPerformed(evt);
					}
				});
			}
			{
				searchTextArea = new JTextArea();
				this.add(searchTextArea);
				searchTextArea.setText("For a search, enter part of the movie name:");
				searchTextArea.setBounds(28, 422, 261, 18);
				searchTextArea.setOpaque(false);
				searchTextArea.setFont(new java.awt.Font("Arial",1,12));
				searchTextArea.setEditable(false);
			}
			{
				searchButton = new JButton();
				this.add(searchButton);
				searchButton.setText("Search");
				searchButton.setBounds(214, 449, 78, 21);
				searchButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						setSearchList(searchTextField.getText());
						logic.ToGuiFacade.getInstance().nextTenMoviesToRate(_searchMovies, 0);
					}
				});
			}
			{
				allButton = new JButton();
				this.add(allButton);
				allButton.setText("Complete List");
				allButton.setBounds(314, 449, 120, 20);
				allButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						setMovieList();
						logic.ToGuiFacade.getInstance().nextTenMoviesToRate(_searchMovies, 0);
					}
				});
			}
			{
				previousButton = new JButton();
				this.add(previousButton);
				previousButton.setText("Previous 10 movies");
				previousButton.setBounds(28, 395, 191, 21);
				previousButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						previousButtonActionPerformed(evt);
					}
				});
			}
			{
				backButton = new JButton();
				this.add(backButton);
				backButton.setText("back");
				backButton.setBounds(625, 294, 63, 21);
				backButton.setBounds(616, 457, 72, 21);
				backButton.addMouseListener(new MouseAdapter() {
					public void mouseReleased(MouseEvent evt) {
						ToGuiFacade fac= ToGuiFacade.getInstance();
						fac.back();
					}
				});
			}
			{
				searchTextField = new JTextField();
				this.add(searchTextField);
				searchTextField.setBounds(28, 449, 174, 21);
			}
			ToGuiFacade.getInstance().setRecommendationListScreen(this);
			setMovieList();
			logic.ToGuiFacade.getInstance().nextTenMoviesToRate(_searchMovies, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	protected void rateButtonActionPerformed(MouseEvent evt, int tmpnum) {
		String movie = _movieLabels[tmpnum].getText();
		ToGuiFacade.getInstance().showMovieScreen(movie,_user);
	}

	public void setMovieList(){
		_allMovies=Facade.getInstance().getMovies();
		Arrays.sort(_allMovies);
		_searchMovies = _allMovies;
	}

	public void setSearchList(String src){
		_searchMovies = Facade.getInstance().search(src);
	}

	public void setMovies(String[] movs ,int begin){
		TreeSet<RatingTuple> _userRatings=Facade.getInstance().getUserRatings(_user);
		RatingTuple[] ratAr = _userRatings.toArray(new RatingTuple[_userRatings.size()]);
		String[] _userMovies =new String[ratAr.length];
		for(int i=0; i<ratAr.length; i++){
			_userMovies[i] = ratAr[i].get_movie().get_name();
		}
		
		for (int i=0; i<10; i++){
			_movieLabels[i].setText("");
			_numLabels[i].setText("");
			_scoresLabels[i].setText("");
			_rateButtons[i].setVisible(false);
		}
		
		for (int i=0; i<movs.length-begin&& i<10; i++){
			boolean flag =false;
			if(_userMovies!=null){
				for(int j=0; j<_userMovies.length; j++){
					if(movs[i+begin].equals(_userMovies[j])){
						flag =true;
						break;
					}
				}
			}
			_movieLabels[i].setText(movs[i+begin]);
			_numLabels[i].setText((new Integer(begin+i+1)).toString());
			if(flag){
				_scoresLabels[i].setText("RATED");
				_scoresLabels[i].setForeground(Color.BLUE);
			}
			_rateButtons[i].setVisible(true);
		}
	}

	private void next10ButtonActionPerformed(ActionEvent evt) {
		if(!_numLabels[9].getText().equals("")){
			ToGuiFacade.getInstance().nextTenMoviesToRate(_searchMovies,new Integer (_numLabels[9].getText()));
		}

	}

	private void previousButtonActionPerformed(ActionEvent evt) {
		int i =(new Integer (_numLabels[0].getText())-11);
		if (i<0)
			i=0;
		ToGuiFacade.getInstance().nextTenMoviesToRate(_searchMovies,i);

	}


}
